/*
 * Copyright (c) 2010 Network Security Lab (NSL), Department of Electrical
 *                    Engineering, University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: He Wu mdzz@uw.edu
 */

/**
 * @file util.c
 */

#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "util.h"

/******************************************************************************/

static __u16 localAddr = 0; //!< local address for current node
static int isBase = 0;      //!< base flag

/******************************************************************************/

int set_addr()
{
  FILE *fp;
  // read local address form config file
  fp = fopen("/root/router.conf", "r");
  if (fp == NULL) {
    printf("Router config file missing.\n");
    return -1;
  }
  else {
    fscanf(fp, "%hu\n%d\n", &localAddr, &isBase);
    fclose(fp); // close file
  }
  return 0;
}


__u16 get_addr()
{
  return localAddr;
}


int check_base()
{
  return isBase;
}


__u32 skew(__u32 base)
{
  assert(base > 1);
  __s32 res = (rand() % base) / 3; // 1/3 of original value
  res *= pow(-1, rand() % 2);
  return (__u32 ) (base + res);
}
